$(function() {
	  
	  var table = $('#example').DataTable( {
		      columns: [
 					{ title: "用户编号","data" : "userId"},
		            { title: "用户名","data": "userName" },
		            { title: "密码","data": "userPassword"},
		            { title: "状态","data": "status"},
		            { title: "手机号码","data": "mobile" },
		            { title: "创建时间","data": "createTime" }
		               ],
             // "sScrollX": "100%",//水平显示宽度
             // "sScrollXInner": "100%",//水平总宽度
             // "bScrollCollapse": true,
              "columnDefs": [
              {
                  "render": function(data, type, row, meta) {
                      //渲染数据
                     		 return data === 0 ? 
          					'<span class="label label-danger">禁用</span>' : 
          					'<span class="label label-success">正常</span>';
                  },
                  //指定是第三列
                  "targets": 3
              },
              {
                  "render": function(data, type, row, meta) {
                      //渲染数据
                     		 return moment(data).format('YYYY-MM-DD HH:mm:ss');;
                  },
                  //指定是第5列
                  "targets": 5
              }
              
              
              
              ],
              
	        "language": {
                "lengthMenu": "每页 _MENU_ 条记录",
                "zeroRecords": "没有找到记录",
                "info": "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )",
                "infoEmpty": "无记录",
                "infoFiltered": "(从 _MAX_ 条记录过滤)",
                "search":"查询",
                "paginate": {  
                    "next":       "下一页",  
                    "previous":   "上一页"  
                }
                
            },
           
            "ajax": '/getuser'
	    } );
	  $('#example tbody').on( 'click', 'tr', function () {
	        if ( $(this).hasClass('selected') ) {
	            $(this).removeClass('selected');
	        }
	        else {
	            table.$('tr.selected').removeClass('selected');
	            $(this).addClass('selected');
	        }
	    } );
	 //保存用户
	 $("#save").on('click',function(){
		 var username = $("#username").val();//用户名
		 var password = $("#password").val();//密码
		 var tel = $("#tel").val();//手机
		 var status = $('input[name="status"]:checked').val();//状态
		 var role_array = $("#sel_menu2").val();//角色
		 $.ajax({
			    type: 'POST',
			    url: "/adduser",
			    data: {
			    	username: username,
			    	password:password,
			    	tel:tel,
			    	status:status,
			    	role:role_array[0]
			    },
			    dataType: "json",
			    success: function(data){
			    	console.log(data);
			    	new PNotify({
			             title: '提示',
			             text: '添加成功',
			             type: 'success',
			             delay: 1000, 
			             styling: 'bootstrap3',
			             stack: {"dir1": "down", "dir2": "left", "push": "left"}
			         });
			    },
			    error:function(data){
			    	new PNotify({
			             title: '提示',
			             text: '添加失败',
			             type: 'error',
			             delay: 1000, 
			             styling: 'bootstrap3',
			             stack: {"dir1": "down", "dir2": "left", "push": "left"}
			         });
			    }
			});
		 

	 });
	 
	 $('input.flat').iCheck({
         checkboxClass: 'icheckbox_flat-green',
         radioClass: 'iradio_flat-green'
     });
} );

$(document).ready(function() {
	//var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];

	//多选
    $("#sel_menu2").select2({
						        tags: true,
						        placeholder: "请选择角色",
						        language: "zh-CN",//中文提示
						        maximumSelectionLength: 1, //最多能够选择的个数
						        ajax: {
									            url: '/formgetrole',
									            processResults: function (resp) {
															            	var array = new Array();
																            	for (var i = 0; i < resp.length; i++)
																				{
																					var role = resp[i];
																					array.push({id:role.roleId, text: role.roleName});
																				}
															            	var ret = new Object();
															    			ret.results = array;
															    			return ret
																			}
									
						            }
                           });
  
})
